package com.mooral.web.tag.html;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.mooral.context.Context;
import com.mooral.context.ContextSession;

public abstract class MooralTag extends BodyTagSupport {


	  private Context context = null;

	  /**
	   * Start del tag especializado.
	   */
	  public abstract int doStartTag(HttpServletRequest request,Context context) throws JspException;

	  /**
	   * End del tag especializado.
	   */
	  public abstract int doEndTag(HttpServletRequest request,Context context) throws JspException;

	  /**
	  *  Start del tag
	  */
	  public final int doStartTag() throws JspException
	  {
	    // Request
	    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

	    // Contexto
		context = ContextSession.getCurrentContext();


	    return doStartTag(request,context);
	  }

	  /**
	  *  End del tag
	  */
	  public final int doEndTag() throws JspException
	  {
	    // Request
	    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

	    // Contexto
	    context = ContextSession.getCurrentContext();

	    return doEndTag(request,context);
	  }

	  /**
	  * Release del tag
	  */
	  public void release()
	  {
	    super.release();
	  }




	  /**
	   * Escribe en el response.
	   */
	  public void write(Object object) throws JspException
	  {
	    write( pageContext,object);
	  }

	  /**
	   * Escribe en el PageContext
	 * @throws IOException 
	   */
	  public void write(PageContext pageContext, Object object) throws JspException
	  {
/*		  JspWriter out = pageContext.getOut();
		  try {
			  if(object == null)
					out.write((String)object);
			  else
				  out.write(object.toString());
		  } catch (IOException e) {
				throw new JspException(e);
		  }
	*/	  
	        JspWriter writer = pageContext.getOut();

	        try {
			  if(object == null)
				  writer.print((String)object);
			  else
				  writer.print(object.toString());

	        } catch (IOException e) {
	            throw new JspException(e.toString());
	        }
		  
	  }

}
